﻿#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QMessageBox>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#pragma execution_character_set("utf-8")


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setFixedSize(452,757);
    setWindowFlag(Qt::FramelessWindowHint);

    myMenu = new QMenu(this);
    myMenu->setStyleSheet("QMenu::item {color:white}");
    QAction *action = new QAction(QIcon(":/rec/poweroff.png"),tr("退出"),this);
    myMenu->addAction(action);
    connect(myMenu,&QMenu::triggered,this,[=](){
        this->close();
    });

    manager = new QNetworkAccessManager(this);
    strUrl = "http://v1.yiketianqi.com/api?unescape=1&version=v61&appid=58664185&appsecret=2ANeZ7RJ";
    QUrl url(strUrl);
    QNetworkRequest req(url);
    manager->get(req);
    connect(manager,&QNetworkAccessManager::finished,this,&Widget::onReadData);

}

Widget::~Widget()
{
    delete ui;
}
void Widget::mousePressEvent(QMouseEvent *event)
{

    if(event->button() == Qt::RightButton){
        myMenu->exec(QCursor::pos());
    }
    offset = event->globalPos()-this->pos();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - offset);
}

void Widget::onReadData(QNetworkReply *reply)
{
    int resCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    qDebug() << resCode;
    if(reply->error() == QNetworkReply::NoError && resCode == 200){

        QByteArray bty = reply->readAll();
        parseWeatherJsonData(bty);
        qDebug() << QString::fromUtf8(bty);

    }else {

        QMessageBox msgBox;
        msgBox.setText("The document has been modified.");
        msgBox.exec();

    }
}

void Widget::parseWeatherJsonData(QByteArray rawData)
{
    QJsonDocument jsonObj = QJsonDocument::fromJson(rawData);
    if(!jsonObj.isNull() && jsonObj.isObject()){
        QJsonObject objRoot = jsonObj.object();
        QString date = objRoot["date"].toString();
        QString week = objRoot["week"].toString();
        ui->lb_date->setText(date + "  " + week);
        ui->lb_local->setText(objRoot["city"].toString());
        ui->lb_temp->setText(objRoot["tem"].toString());
        ui->lb_tempbetween->setText(objRoot["tem1"].toString()+"~"+objRoot["tem2"].toString());
        ui->lb_weather->setText(objRoot["wea"].toString());

        ui->lb_tips->setText(objRoot["air_tips"].toString());

        ui->lb_wind->setText(objRoot["win"].toString());
        ui->lb_wind2->setText(objRoot["win_speed"].toString());

        ui->lb_hum_2->setText(objRoot["humidity"].toString());

        ui->lb_pm25_2->setText(objRoot["air_pm25"].toString());
        ui->lb_airqulity2->setText(objRoot["air_level"].toString());
    }
}

QString Widget::getCitycodeFromUser(QString name)
{
    // 遍历文件，找到对应ID
    QFile file(":/local/local.json");
    file.open(QIODevice::ReadOnly);
    QByteArray rawData = file.readAll();
    file.close();

    QJsonDocument jsonDoc = QJsonDocument::fromJson(rawData);
    if(jsonDoc.isArray()){
        QJsonArray citys = jsonDoc.array();
        for(QJsonValue val : citys){
            if(val.isObject()){
                QString cityName = val["cityName"].toString();
                if(cityName == name){
                    return val["cityCode"].toString();
                }
            }
        }
    }
    return "";
}


void Widget::on_pb_search_clicked()
{
    // 获取输入框内容
    QString cityname = ui->le_search->text();
    QString cityCode = cityCodeUtils.getCodeFromName(cityname);
    if(cityCode != NULL){
        strUrl += "&cityid=" + cityCode;
        manager->get(QNetworkRequest(QUrl(strUrl)));
    }else {
        QMessageBox msgBox;
        msgBox.setText("input error");
        msgBox.exec();
    }
}
